﻿' Copyright (c) Microsoft Corporation.  All rights reserved.

Partial Public Class NewBlogPost
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            Return
        End If

        Dim personID = 0
        Integer.TryParse(Request.Params("PersonID"), personID)

        ' create a MyTravelEntities
        Using travelEntities As New MyTravelEntities()
            ' get the person and attach the person to view state for later.
            Dim blogowner = travelEntities.People.First(Function(p) p.PersonID = personID)

            ' if the blogowner existed.
            If Not blogowner Is Nothing Then
                ViewState("Person") = blogowner

                ' get all the blogs, save the blogs in view state.
                blogowner.Blogs.Load()
                ViewState("Blogs") = blogowner.Blogs.ToList()

                ' get the series title.
                dropDownBlogSeries.DataTextField = "SeriesTitle"
                dropDownBlogSeries.DataSource = blogowner.Blogs
                dropDownBlogSeries.DataBind()
            End If

            calendarDateVisited.SelectedDate = DateAndTime.Now
        End Using
    End Sub

    Protected Sub buttonSavePost_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonSavePost.Click
        ' create a new blog post to the blog series selected.
        Using travelEntities As New MyTravelEntities()
            ' get the blog out of view state, and get the list of items.
            ' select the index and attach to the travel entities.
            Dim blogList = ViewState("Blogs")
            Dim seriesBlog As Blog

            Dim newBlogName = textNewBlog.Text
            Dim blogAuthor = ViewState("Person")

            ' if the blog name is empty, get the currently selected blog.
            If 0 = newBlogName.Length Then
                seriesBlog = blogList(dropDownBlogSeries.SelectedIndex)
                If seriesBlog Is Nothing Then
                    ' probably should put an error here if this wasn't an example.
                    Return
                End If

                ' attach to the travel entities.
                travelEntities.Attach(seriesBlog)
            Else
                ' create a new blog and add to the travel entities.
                seriesBlog = New Blog()
                seriesBlog.SeriesTitle = newBlogName

                ' attach the person
                travelEntities.Attach(blogAuthor)

                ' set this to the blog author
                blogAuthor.Blogs.Add(seriesBlog)
            End If


            ' create a new blog post.
            Dim newPost As New BlogPost() With { _
                                            .BlogDate = DateTime.Now, _
                                            .BlogTitle = textPostTitle.Text, _
                                            .CityVisited = textCity.Text, _
                                            .CountryVisited = textCountry.Text, _
                                            .DateVisited = calendarDateVisited.SelectedDate, _
                                            .BlogEntry = textBlogPost.Text}

            ' add to the blog series. This adds to the objectstatemanager
            ' for tracking.
            seriesBlog.BlogPosts.Add(newPost)

            Try
                travelEntities.SaveChanges()
            Catch ex As OptimisticConcurrencyException
                ' catching this exception allows you to 
                ' refresh entities with either store/client wins
                ' project the entities into this failed entities.
                Dim failedEntities = From e1 In ex.StateEntries _
                                     Select e1.Entity

                ' Note: in future you should be able to just pass the opt.StateEntities in to refresh.
                travelEntities.Refresh(RefreshMode.ClientWins, failedEntities.ToList())
                travelEntities.SaveChanges()
            End Try

            ' clear out and then go to the travelpage.
            Response.Redirect("TravelPage.aspx?BlogID=" & seriesBlog.BlogID.ToString())
        End Using
    End Sub
End Class